<template>
  <ele-modal
    :width="550"
    :visible="visible"
    :confirm-loading="loading"
    :title="title"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      :label-col="{ md: { span: 5 }, sm: { span: 4 }, xs: { span: 24 } }"
      :wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
    >
      <a-row :gutter="24">
        <a-col :md="24" :sm="24" :xs="24">
          <a-form-item label="角色名称：" v-bind="validateInfos.name">
            <a-input
              allow-clear
              placeholder="请输入"
              v-model:value="form.name"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="角色描述：">
            <a-textarea
              allow-clear
              v-model:value="form.description"
              placeholder="请输入"
              :rows="4"
              autocomplete="off"
            />
          </a-form-item>
          <a-form-item label="是否可登录后台：">
            <a-switch v-model:checked="checked" @change="change" />
          </a-form-item>
          <a-form-item label="是否启用：">
            <a-switch v-model:checked="checked2" @change="change2" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Form, message } from 'ant-design-vue';
  import { detail } from '@/api/user/store_role';
  // 表格列配置
  const useForm = Form.useForm;

  const emit = defineEmits(['done', 'update:visible']);

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    editDataId: [String, Number]
  });

  // 提交状态
  const loading = ref(false);
  const title = ref('新建');
  const name = ref(null);
  const checked = ref(true);
  const checked2 = ref(true);

  // 表单数据
  const form = reactive({
    role_id: '',
    name: '',
    description: '',
    access: '1',
    status: '1'
  });

  onMounted(() => {
    form.role_id = props.editDataId;
    if (props.editDataId) {
      title.value = '修改';
      detail({ role_id: props.editDataId }).then((res) => {
        Object.keys(form).map((key) => {
          form[key] = res && res[key] ? String(res[key]) : '';
        });
        form.role_id = res.id;
        checked.value = form.access == 1 ? true : false;
        checked2.value = form.status == 1 ? true : false;
      });
    }
  });
  const change = (e) => {
    form.access = e ? '1' : '0';
  };
  const change2 = (e) => {
    form.status = e ? '1' : '0';
  };

  // 表单验证规则
  const rules = reactive({
    name: [
      {
        required: true,
        message: '请输入角色名称',
        type: 'string',
        trigger: 'change'
      }
    ]
  });

  const { validate, validateInfos } = useForm(form, rules);

  /* 保存*/
  const save = () => {
    validate()
      .then(() => {
        emit('done', form);
      })
      .catch(() => {});
  };
  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };
</script>
<style scoped></style>
